//package com.zws.cucumber.optimization.socket.connector;
//
//import com.zws.cucumber.optimization.ZwsOptimizations;
//import com.zws.cucumber.optimization.socket.ZwsSockets;
//import org.eclipse.jetty.server.LocalConnector;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.util.ReflectionUtils;
//
//import java.io.IOException;
//import java.lang.reflect.Method;
//import java.nio.ByteBuffer;
//import java.util.concurrent.TimeUnit;
//
//public final class ZwsConnectorJettyAdaptor implements ZwsConnectorAdaptor {
//
//    private static final Logger logger = LoggerFactory.getLogger(ZwsConnectorJettyAdaptor.class);
//
//    private static final Method methodOfExecuteRequest;
//
//    static {
//        methodOfExecuteRequest = ReflectionUtils.findMethod(LocalConnector.class, "executeRequest", ByteBuffer.class);
//        methodOfExecuteRequest.setAccessible(true);
//    }
//
//    private LocalConnector localConnector;
//    private ZwsCallerExecutionJettyConnector executionInMainThreadLocalConnector;
//    private final Runnable acceptorRunnable = () -> {
//        Thread thread = Thread.currentThread();
//        String name = thread.getName();
//        try {
//            thread.setName("main-jetty");
//            executionInMainThreadLocalConnector.accept(0);
//        } catch (InterruptedException | IOException e) {
//            throw new RuntimeException(e);
//        } finally {
//            thread.setName(name);
//        }
//    };
//    private final ZwsConnectorAdaptor func;
//    private final ZwsConnectorAdaptor executionInMainThreadFunc;
//
//    public ZwsConnectorJettyAdaptor(LocalConnector localConnector) {
//        this.localConnector = localConnector;
//        this.func = localConnector::getResponse;
//        this.executionInMainThreadFunc = null;
//    }
//
//    public ZwsConnectorJettyAdaptor(LocalConnector localConnector, ZwsCallerExecutionJettyConnector executionInMainThreadLocalConnector) {
//        this.localConnector = localConnector;
//        this.func = localConnector::getResponse;
//
//        this.executionInMainThreadLocalConnector = executionInMainThreadLocalConnector;
//        this.executionInMainThreadFunc = (buf, head, time, unit) -> {
//            executionInMainThreadLocalConnector.setInvoking(true);
//            LocalConnector.LocalEndPoint endp = (LocalConnector.LocalEndPoint) ReflectionUtils.invokeMethod(methodOfExecuteRequest, executionInMainThreadLocalConnector, buf);
//            executionInMainThreadLocalConnector.getExecutor().execute(acceptorRunnable);
//            return endp.waitForResponse(head, time, unit);
//        };
//    }
//
//    @Override
//    public ByteBuffer getResponse(ByteBuffer requestBuffer, boolean head, long time, TimeUnit unit) throws Exception {
//        // when upgrade to jdk21, is it possible to make it in virtual thread?
//        if (ZwsOptimizations.isJettyExecutionInMainThreadEnabled() && ZwsSockets.isMainThread()) {
//            return (ByteBuffer) executionInMainThreadFunc.getResponse(requestBuffer, head, time, unit);
//        }
//        return (ByteBuffer) func.getResponse(requestBuffer, head, time, unit);
//    }
//
//}
